﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    public partial class BizDonViDaoTao
    {
        /// <summary>
        /// Lấy danh sách
        /// </summary>
        /// <returns></returns>
        public static List<DonVi> GetList()
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                List<DonVi> ls = db.DonViDbSet.OrderBy(e => e.MaDonVi).ToList();
                return ls;
            }
        }

        public static DonVi GetByID(string guidIdDonVi, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                    return GetByID(guidIdDonVi, db2);
            else
            {
                return db.DonViDbSet.Where(e => e.MaDonViGUID == guidIdDonVi).SingleOrDefault();
            }
        }

        public static bool IsExistByID(string guidIdDonVi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.DonViDbSet.Where(e => e.MaDonViGUID == guidIdDonVi).SingleOrDefault() == null ? false : true;
            }
        }

        /// <summary>
        /// Kiểm tra Mã có tồn tại hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByCode(string maDonVi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.DonViDbSet.Where(e => e.MaDonVi == maDonVi).SingleOrDefault() == null ? false : true;
            }
        }

        /// <summary>
        /// Kiểm tra tên có trùng hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByName(string tenDonVi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.DonViDbSet.Where(e => e.TenDonVi == tenDonVi).SingleOrDefault() == null ? false : true;
            }
        }

        public static CODE_RESULT_RETURN Add(DonVi obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _donVi = db.DonViDbSet.Where(e => e.TenDonVi == obj.TenDonVi).FirstOrDefault();
                if (_donVi != null)
                    return CODE_RESULT_RETURN.TenTrung;
                _donVi = db.DonViDbSet.Where(e => e.MaDonVi == obj.MaDonVi).FirstOrDefault();
                if (_donVi != null)
                    return CODE_RESULT_RETURN.MaTrung;
                obj.MaDonViGUID = Guid.NewGuid().ToString();
                db.DonViDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DonVi>(nhatKy, null, obj.MaDonViGUID, LOAIDOITUONG.ĐƠN_VỊ_ĐÀO_TẠO, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<DonVi>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(DonVi obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _donVi = db.DonViDbSet.Where(e => e.TenDonVi == obj.TenDonVi && e.MaDonViGUID != obj.MaDonViGUID).FirstOrDefault();
                if (_donVi != null)
                    return CODE_RESULT_RETURN.TenTrung;
                _donVi = db.DonViDbSet.Where(e => e.MaDonVi == obj.MaDonVi && e.MaDonViGUID != obj.MaDonViGUID).FirstOrDefault();
                if (_donVi != null)
                    return CODE_RESULT_RETURN.MaTrung;
                _donVi = db.DonViDbSet.Where(e => e.MaDonViGUID == obj.MaDonViGUID).FirstOrDefault();
                if (_donVi == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DonVi>(nhatKy, _donVi, obj.MaDonViGUID, LOAIDOITUONG.ĐƠN_VỊ_ĐÀO_TẠO, LOAITHAOTAC.Chỉnh_sửa);

                _donVi.MaDonVi = obj.MaDonVi;
                _donVi.TenDonVi = obj.TenDonVi;
                _donVi.TruongDonVi = obj.TruongDonVi;
                _donVi.SoDienThoai = obj.SoDienThoai;
                _donVi.GhiChu = obj.GhiChu;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<DonVi>(nhatKy, _donVi);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(string guidIdDonVi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _donVi = db.DonViDbSet.Where(e => e.MaDonViGUID == guidIdDonVi).FirstOrDefault();
                if (_donVi == null)
                    return CODE_RESULT_RETURN.ThatBai;
                if (!(_donVi.ListNganhDaoTao != null && _donVi.ListNganhDaoTao.Count > 0) || (_donVi.ListHocPhan != null && _donVi.ListHocPhan.Count > 0) || (_donVi.ListGiangVien != null && _donVi.ListGiangVien.Count > 0))
                {
                    db.DonViDbSet.Remove(_donVi);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<DonVi>(nhatKy, _donVi, _donVi.MaDonViGUID, LOAIDOITUONG.ĐƠN_VỊ_ĐÀO_TẠO, LOAITHAOTAC.Xóa);
                    BizNhatKyHoatDong.Add<DonVi>(nhatKy, null);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                    return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }

}
